/* eslint-disable @typescript-eslint/no-explicit-any */
import React, { useState } from "react";
import { DesktopOutlined } from "@ant-design/icons";
import type { MenuProps } from "antd";
import { Breadcrumb, Layout, Menu, theme } from "antd";
import { Childrenroutes } from "../router";
import { NavLink, Outlet } from "react-router-dom"; // 假设你使用的是react-router
import { ItemType, MenuItemType } from "antd/es/menu/hooks/useItems";
import { JSX } from "react/jsx-runtime";
import './home.css'
const { Header, Content, Footer, Sider } = Layout;

type MenuItem = Required<MenuProps>["items"][number];

function getItem(
  label: React.ReactNode,
  key: React.Key,
  icon?: React.ReactNode,
  children?: MenuItem[]
): MenuItem {
  return {
    key,
    icon,
    children,
    label,
  } as MenuItem;
}

const MenuItem:
  | ItemType<MenuItemType>[]
  | {
      key: any;
      path: any;
      label: any;
      icon: JSX.Element;
      // 如果存在子路由，则为该菜单项添加 children 属性
      children: any;
    }[]
  | undefined = [];
function buildMenuItems(routes: any[], parentPath = "") {
  routes.forEach((route) => {
    const item = {
      key: route.path,
      path: route.path,
      label: <NavLink to={route.path}>{route.title}</NavLink>,
      icon: <DesktopOutlined />,
      // 如果存在子路由，则为该菜单项添加 children 属性
      children: route.children
        ? route.children.map((child: { path: any; title: any ;children?: any[] }) => ({
            key: `${parentPath}${child.path}`,
            path: child.path,
            label: <NavLink to={child.path}>{child.title}</NavLink>,
            icon: <DesktopOutlined />,
            children: child.children
        ? child.children.map((son: { path: any; title: any }) => ({
            key: `${parentPath}${son.path}`,
            path: son.path,
            label: <NavLink to={son.path}>{son.title}</NavLink>,
            icon: <DesktopOutlined />,
            
          }))
        : undefined,
          }))
        : undefined,
    };

    // 将菜单项添加到数组中
    MenuItem.push(item);

    // 如果存在子路由，递归处理子路由
    // if (route.children) {
    //   buildMenuItems(route.children, `${parentPath}${route.path}/`);
    // }
  });
}

// 调用函数来构建菜单项数组
buildMenuItems(Childrenroutes);

// const items: MenuItem[] = [

//   getItem('Option 1', '1', <PieChartOutlined />),
//   getItem('Option 2', '2', <DesktopOutlined />),
//   getItem('User', 'sub1', <UserOutlined />, [
//     getItem('Tom', '3'),
//     getItem('Bill', '4'),
//     getItem('Alex', '5'),
//   ]),
//   getItem('Team', 'sub2', <TeamOutlined />, [getItem('Team 1', '6'), getItem('Team 2', '8')]),
//   getItem('Files', '9', <FileOutlined />),
// ];

const App: React.FC = () => {
  const [collapsed, setCollapsed] = useState(false);
  const {
    token: { colorBgContainer, borderRadiusLG },
  } = theme.useToken();

  return (
    <Layout style={{ minHeight: "100vh" }}>
      <Sider
        collapsible
        collapsed={collapsed}
        onCollapse={(value) => setCollapsed(value)}
      >
        <div className="demo-logo-vertical" />
        <Menu
          theme="dark"
          defaultSelectedKeys={["1"]}
          mode="inline"
          items={MenuItem}
        />
      </Sider>
      <Layout>
        <Header style={{ padding: 0, background: colorBgContainer }} />
        <Content style={{ margin: "0 16px" }}>
          <Breadcrumb style={{ margin: "16px 0" }}>
            <Breadcrumb.Item>User</Breadcrumb.Item>
            <Breadcrumb.Item>Bill</Breadcrumb.Item>
          </Breadcrumb>
          <div
            style={{
              padding: 24,
              minHeight: 360,
              background: colorBgContainer,
              borderRadius: borderRadiusLG,
            }}
          >
            <Outlet />
          </div>
        </Content>
        <Footer style={{ textAlign: "center" }}>
          Ant Design ©{new Date().getFullYear()} Created by Ant UED
        </Footer>
      </Layout>
    </Layout>
  );
};

export default App;
